package com.zhz.project.main.socolar.issue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhz.project.common.util.alibaba.StringUtil;
import com.zhz.project.common.util.ibatispage.IbatisPage;
import com.zhz.project.dal.daointerface.IssueDAO;
import com.zhz.project.dal.dataobject.IssueDO;
import com.zhz.project.dal.dataobject.QikanDO;
import com.zhz.project.util.getWebContent;

public class SocolarIssueMain {

    public static String socolar = "http://www.socolar.com/";

    public static void main(String[] args) {

        SocolarIssueMain socolarMain = new SocolarIssueMain();
        socolarMain.parse();
    }

    public void testParse() {
    }

    public void parse() {
        ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
            new String[] { "ibatis-spring.xml" });

        IssueDAO issueDAO = (IssueDAO) appContext.getBean("issueDAO");
        IbatisPage page = (IbatisPage) appContext.getBean("ibatisPage");

        Map map = new HashMap();
        map.put("indexPage", "1");

        Map tocdoajmap = page.getPage(map, "selectQikanCount", "selectQikanByPage");
        List<QikanDO> qikanDOlist = (List<QikanDO>) tocdoajmap.get("list");

        for (QikanDO qikanDO : qikanDOlist) {
            SocolarIssueParser parser = new SocolarIssueParser();
            parser.setContent(getWebContent.getContent(SocolarIssueMain.socolar
                                                       + qikanDO.getSocolarurl()));

            List<SocolarYearRecord> socolarYearRecordList = parser.parse();

            for (SocolarYearRecord socolarYearRecord : socolarYearRecordList) {
                for (SocolarIssueRecord socolarIssueRecord : socolarYearRecord
                    .getSocolarIssueRecordList()) {
                    IssueDO issueDO = new IssueDO();
                    issueDO.setYear(socolarYearRecord.getYear());
                    issueDO.setIssue(socolarIssueRecord.getIssue());
                    issueDO.setUrl(StringUtil.substring(socolarIssueRecord.getUrl(), 0,
                        StringUtil.indexOf(socolarIssueRecord.getUrl(), "&key")));
                    System.out.println(socolarIssueRecord.getUrl());
                    issueDO.setQikanid(String.valueOf(qikanDO.getId()));
                    //                    System.out.println(issueDO);
                    issueDAO.insert(issueDO);
                }

            }

        }
    }
}
